<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 分类
 *
 * @author      Eden
 * @datetime    2016/11/27 16:00
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Category extends ADM_Controller
{
    /**
     * 初始化
     */
    public function __construct ()
    {
        parent::__construct();
    }


    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/goods/category.html');
    }

    /**
     * 获取列表
     */
    public function getLists()
    {
        $this->load->model('admin/common/Common_Model', 'com');
        $rows = $this->com->getAllGoodsCategories();

        $parents = $son = [];
        if (!empty($rows)) foreach($rows as $item){
            if ($item['parentId'] == 0) {
                $item['subCats'] = [];
                $parents[] = $item;
            } else {
                $pId = $item['parentId'];
                !isset($son[$pId]) && $son[$pId] = [];
                $son[$pId][] = $item;
            }
        }

        foreach($parents as &$p) {
            $id = $p['id'];
            $p['subCats'] = isset($son[$id]) ? $son[$id] : [];
        }

        $send['state'] = true;
        $send['data'] = $parents;
        $this->output->send($send);
    }

    /**
     * 修改分类
     */
    public function resetCats()
    {
        $send['state'] =false;
        do {

            $params = $this->input->post();
            if (empty($params['catId'])) {
                $send['error'] = '参数错误';
                break;
            }

            if (isset($params['name']) && empty($params['name'])) {
                $send['error'] = '分类名称不能为空';
                break;
            }

            $this->load->model('admin/goods/Category_Model', 'cat');
            $res = $this->cat->resetCats($params);
            if (!$res) {
                $send['error'] = '修改失败';
            } else {
                $send['state'] = true;
                $send['data'] = $res;
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 添加分类
     */
    public function addCat()
    {
        $send['state'] =false;
        do {

            $params = $this->input->post();
            if (empty($params['name'])) {
                $send['error'] = '分类名称不能为空';
                break;
            }

            $this->load->model('admin/goods/Category_Model', 'cat');
            $res = $this->cat->addCat($params, (isset($params['parentId']) ? 'son' : 'parent'));
            if (!$res) {
                $send['error'] = '添加失败';
            } else {
                $send['state'] = true;
                $send['data'] = $res;
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除
     */
    public function remove()
    {
        $send['state'] =false;
        do {

            $params = $this->input->get();
            if (empty($params['id'])) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/goods/Category_Model', 'cat');
            $res = $this->cat->removeCat($params['id']);
            if (!$res) {
                $error = $this->cat->getError();
                $send['error'] = !empty($error) ? $error : '删除失败';
            } else {
                $send['state'] = true;
            }
        } while(false);
        $this->output->send($send);
    }

}
